package com.cgsd.heyu.ui.task.adapter;

import com.cgsd.heyu.R;
import com.cgsd.heyu.ui.task.vo.OptionVo;
import com.cgsd.heyu.utils.Selectable;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * Author：sky on 2021/3/24 21:53.
 * Email：xcode126@126.com
 * Desc：
 */
public class MultiOptionAdapter extends BaseQuickAdapter<OptionVo, BaseViewHolder> implements Selectable<OptionVo> {

    private List<OptionVo> sectionList;

    public MultiOptionAdapter(List<OptionVo> list) {
        super(R.layout.item_option_multi, list);
        sectionList = new ArrayList<>();
    }

    @Override
    protected void convert(BaseViewHolder helper, OptionVo item) {
        helper.setText(R.id.tv_content, item.text);
        helper.setGone(R.id.iv_right,isSelected(item));
    }

    @Override
    public boolean isSelected(OptionVo item) {
        return sectionList.contains(item);
    }

    @Override
    public void toggleSelection(OptionVo item) {
        if (isSelected(item)) {
            sectionList.remove(item);
        } else {
            sectionList.add(item);
        }
        notifyDataSetChanged();
    }

    @Override
    public void clearSelection() {
        sectionList.clear();
    }

    @Override
    public int getSelectedCount() {
        return sectionList.size();
    }

    @Override
    public List<OptionVo> getSelection() {
        return sectionList;
    }

    /**
     * get param
     * @return
     */
    public String getParamId() {
        StringBuilder sb = null;
        for (OptionVo vo : sectionList) {
            if (sb == null) {
                sb = new StringBuilder(vo.id);
            } else {
                sb.append(",").append(vo.id);
            }
        }
        return sb.toString();
    }

    /**
     * get param
     * @return
     */
    public String getParamTxt() {
        StringBuilder sb = null;
        for (OptionVo vo : sectionList) {
            if (sb == null) {
                sb = new StringBuilder(vo.text);
            } else {
                sb.append(",").append(vo.text);
            }
        }
        return sb.toString();
    }
}
